$(document).ready(function(){
    $('#begin_match').click(function(){
        $('.input-job-container').toggleClass('hide');
        $('.match-result-container').toggleClass('hide');
        $('#begin_match').toggleClass('hide');
        $('.job-description textarea').val('');
        $('#job-name').val('');
    });

    var match_result = [];
    var showing_index = 0;

    $('#confirm').click(function(){
        var title = $('#job-name').val();
        var desc = $('.job-description textarea').val();

        if(title.length == 0 || desc.length == 0){
            alert('请您填写职位名称和职位描述');
            return;
        }

        $('#confirm').text('正在匹配，请稍等...')

        $.ajax({
            'url':'/resum_job_match/match.do',
            'dataType': 'json',
            'data':{
                'title':title,
                'desc':desc
            },
            'success':function(resp){
                if(resp.success){
                    var job_html = '';

                    job_html += '<h2>' + resp.job_title + '</h2>';

                    for(var i = 0;i < resp.job_desc.length;i++){
                        job_html += '<p>' + resp.job_desc[i] + '</p>';
                    }

                    $('.job-desc-wrap').html(job_html);

                    $('.input-job-container').toggleClass('hide');
                    $('.match-result-container').toggleClass('hide');

                    match_result = resp.matched;

                    show_resume(match_result[showing_index]);

                    $('#confirm').text('开始匹配');
                }
            }
        })
    });

    $('#next').click(function(){
        if(match_result.length == 0){
            return;
        }
        showing_index += 1

        if(showing_index == match_result.length){
            showing_index = 0;
        }

        show_resume(match_result[showing_index]);
    });

    $('#prev').click(function(){
        if(match_result.length == 0){
            return;
        }

        showing_index -= 1

        if(showing_index == -1){
            showing_index = match_result.length - 1;
        }

        show_resume(match_result[showing_index]);
    });

    function show_resume(resume){
        var html = '<div style = "height:80px;width:100%;" class = "resume_info"><div class = "fl" style = "width:80%;"><h2>'+resume.wanted_job.title + '</h2><p><span class = "mr10">学历 : '+resume.personal_info.edu + '</span><span class = "mr10">年龄 : '+resume.personal_info.age+'</span><span class = "mr10">经验 : '+resume.personal_info.exp+'</span><span class = "mr10">城市 : '+resume.personal_info.address+'</span></p></div><div class = "fr" style = "width:20%;text-align:right;"><img style = "width:80px;" src = "' + resume.personal_info.head_pic + '" alt = ""></div></div>';

        html += "<div class = 'self_introduce'>" + resume.self_introduce + "</div><div class = 'work_experience'>"

        for(var i = 0;i < resume.work_experience.length;i++){
            html += "<div class = 'work_experience_item'><div class = 'bold'>" + resume.work_experience[i].title + "</div><div class = 'bold' style = 'margin-bottom:5px;'>" + resume.work_experience[i].company + '    ' + '(' + resume.work_experience[i].time + ')' + "</div><div>" + resume.work_experience[i].describe+"</div></div>";
        }

        html += '</div><div class = "proj_experience">';

        for(var i = 0;i < resume.proj_experience.length;i++){
            html += "<div class = 'proj_experience_item'><div class = 'bold'>" + resume.proj_experience[i].title + '   ' + '(' + resume.proj_experience[i].time + ')' + "</div><div>"+resume.proj_experience[i].describe + "</div></div>";
        }

        html += '</div>';

        $('.resume-desc-wrap').html(html);
    }
});